#include "operate.h"

Operate::Operate(QObject *parent) : QObject(parent)
{

}

void Operate::print(QPushButton *b_obj, QLabel *display_obj){
    if(display_obj->text().length() <= 18){
        if(display_obj->text() == "0"){
            num = b_obj->text();
        }else{
            num += b_obj->text();
        }
    }
    display_obj->setText(num);
}

void Operate::print(QPushButton *b_obj, QLabel *display_obj, QLCDNumber * lcd_obj){
    if(display_obj->text().length() <= 18){
        QString f = display_obj->text();
        if(!(display_obj->text().contains(b_obj->text()))){  // 如果label中没有“=”就加一个
            display_obj->setText(display_obj->text() + "=");
        }else{
            f.remove("=");  // 如果label中有“=”就加删除一个“=”
        }
        if(f.contains("+")){
            QStringList strList = f.split("+",QString::SkipEmptyParts);
            int numA = strList.first().toInt();
            int numB = strList.last().toInt();
            Plus *thePlus = new Plus;
            lcd_obj->display(thePlus->caculate(numA, numB));
        }
        if(f.contains("-")){
            QStringList strList = f.split("-",QString::SkipEmptyParts);
            int numA = strList.first().toInt();
            int numB = strList.last().toInt();
            Sub *theSub = new Sub;
            lcd_obj->display(theSub->caculate(numA, numB));
        }
        if(f.contains("*")){
            QStringList strList = f.split("*",QString::SkipEmptyParts);
            int numA = strList.first().toInt();
            int numB = strList.last().toInt();
            Mul *theMul = new Mul;
            lcd_obj->display(theMul->caculate(numA, numB));
        }
    }
}

void Operate::print(QLabel *display_obj, QString sign){
    if(display_obj->text().length() <= 18){
        if(!(display_obj->text().contains(sign))){
            num = display_obj->text();
            num += sign;
            display_obj->setText(num);
        }
    }
}

void Operate::clear(QLabel *display_obj, QLCDNumber * lcd_obj){
    display_obj->setText("0");
    lcd_obj->display(0);
}

void Operate::del(QLabel *display_obj){
    QString str = display_obj->text();
    if(str != "0"){
        if(str.length() == 1){
            display_obj->setText("0");
        }else{
            str.resize(str.length()-1);
            display_obj->setText(str);
        }
    }
}
